﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace KYKJ.Monitor.SystemSetting
{
    /// <summary>
    /// SettingControl.xaml 的交互逻辑
    /// </summary>
    public partial class SystemSetting : UserControl
    {
        #region 字段

        

        #endregion

        public SystemSetting()
        {
            InitializeComponent();
        }

        #region 公开方法

        #endregion

        #region 私有方法

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            bool isChanged = false;
            CameraInfo info = CameraInfo.GetCameraInfo();
            if (info == null)
            {
                MessageBox.Show("保存系统配置失败");
                return;
            }
            if (info.Ip != this.txtIP.Text)
            {
                info.Ip = this.txtIP.Text;
                isChanged = true;
            }
            if (info.Port != Convert.ToInt32(this.txtPort.Text))
            {
                info.Port = Convert.ToInt32(this.txtPort.Text);
                isChanged = true;
            }
            if (info.User != this.txtUser.Text)
            {
                info.User = this.txtUser.Text;
                isChanged = true;
            }
            int streamType = this.rdMain.IsChecked == true ? 0 : 1;
            if (streamType != info.StreamType)
            {
                info.StreamType = streamType;
                isChanged = true;
            }
            if (!string.IsNullOrWhiteSpace(this.txtPwd.Password))
            {
                info.Password = this.txtPwd.Password;
                isChanged = true;
            }
            if (isChanged)
            {
                byte[] byteu = Encoding.Default.GetBytes(info.User);
                info.User = Convert.ToBase64String(byteu);
                byte[] bytep = Encoding.Default.GetBytes(info.Password);
                info.Password = Convert.ToBase64String(bytep);
                bool re = CameraInfo.UpdateCameraInfo(info);
                if (!re)
                {
                    MessageBox.Show("保存系统配置失败");
                    return;
                }
            }
            isChanged = false;
            StorageServerInfo storage = StorageServerInfo.GetStorageServerInfo();
            if (storage == null)
            {
                MessageBox.Show("保存系统配置失败");
                return;
            }
            if (storage.Ip != this.txtIP.Text)
            {
                storage.Ip = this.txtSaveIP.Text;
                isChanged = true;
            }
            if (storage.Port != Convert.ToInt32(this.txtSavePort.Text))
            {
                storage.Port = Convert.ToInt32(this.txtSavePort.Text);
                isChanged = true;
            }
            int isAuto = this.cbAutoSave.IsChecked == true ? 1 : 0;
            if (isAuto != storage.IsAuto)
            {
                storage.IsAuto = isAuto;
                isChanged = true;
            }
            if (isChanged)
            {
                bool re = StorageServerInfo.UpdateStorageServerInfo(storage);
                if (!re)
                {
                    MessageBox.Show("保存系统配置失败");
                    return;
                }
            }
            MessageBox.Show("保存系统配置成功");
        }

        #endregion

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            CameraInfo info = CameraInfo.GetCameraInfo();
            if (info == null)
            {
                MessageBox.Show("读取系统配置失败");
                return;
            }
            this.txtIP.Text = info.Ip;
            this.txtPort.Text = info.Port.ToString();
            this.txtUser.Text = info.User;
            if (info.StreamType == 0)
            {
                this.rdMain.IsChecked = true;
            }
            else
            {
                this.rdSub.IsChecked = true;
            }

            StorageServerInfo storage = StorageServerInfo.GetStorageServerInfo();
            if (storage == null)
            {
                MessageBox.Show("读取系统配置失败");
                return;
            }
            this.txtSaveIP.Text = storage.Ip;
            this.txtSavePort.Text = storage.Port.ToString();
            this.cbAutoSave.IsChecked = storage.IsAuto == 1;
        }
    }
}
